
<div class="p-3 border-x">
    <span class="layui-btn layui-btn-sm add-lawyer">+ 添加律师</span>
</div>
<div>
    <table class="layui-hide" id="lawyer" lay-filter="lawyer"></table>
</div>

<script>
function initLawyer(){
    if($('#caseTab').find('li').eq(0).data('load') == 'true'){
        return false;
    }
    $('#caseTab').find('li').eq(0).data('load','true');

    layui.use(['tool', 'table', 'oaPicker'], function(){
        let tool = layui.tool, table = layui.table, oaPicker = layui.oaPicker;
    
    layui.lawyerTable = table.render({
        elem: '#lawyer',
        title: '律师列表',
        cellMinWidth:80,
        url: '/lawcase/api/get_lawyers',
        where: { case_id: $('#case_id').val() },
        page: true,
        limit: 10,
        cols: [[
            {field: 'lawyer_name', title: '代理律师', width: 180, align:'center'},
            {field: 'lawfirm_name', title: '所属律所'},
            {field: 'update_time', title: '更新时间', align: 'center', templet: function(d){
                return layui.util.toDateString(d.update_time * 1000, 'yyyy-MM-dd HH:mm:ss');}},
            {field: 'is_main', title: '是否主办', align: 'center', templet: function(d){
                return d.is_main == 1 ? '<span class="layui-badge layui-bg-red">主办律师</span>' : '';
            }},
            {fixed: 'right', width: 120, title: '操作', align:'center', templet: function(d){
                return '<div class="layui-btn-group">'+
                    (d.is_main == 1 ? '<a class="layui-btn layui-btn-xs layui-btn-disabled"><span class="layui-badge-dot"></span> 主办律师</a>' : '<a class="layui-btn layui-btn-xs" lay-event="setMain">设为主办</a>')+
                    '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">移除</a>'+
                '</div>';
            }}
        ]]
    });
        
        // 律师选择器
        $('body').on('click','.add-lawyer',function(){
            let opts = {
                title: '选择代理律师',
                url: '/lawcase/api/get_all_lawyers',
                area: ['600px', '580px'],
                cols: [
                    {field: 'lawyer_name', title: '律师姓名', width: 150},
                    {field: 'lawfirm_name', title: '所属律所'}
                ],
                valueField: 'lawyer_id',
                page: true,
                search: true
            };
            
            oaPicker.picker(opts, 1, function(selectData){
                if(selectData.length > 0) {
                    let callback = function(e) {
                        layer.msg(e.msg);
                        if(e.code === 0) {
                            layui.lawyerTable.reload();
                        }
                    };
                    if (!selectData[0] || !selectData[0].lawyer_id) {
                        layer.msg('请选择有效的律师');
                        return;
                    }
                    tool.post("/lawcase/api/addLawyer", {
                        case_id: $('#case_id').val(),
                        lawyer_id: selectData[0].lawyer_id,
                        lawyer_name: selectData[0].lawyer_name,
                        lawfirm_id: selectData[0].lawfirm_id,
                        lawfirm_name: selectData[0].lawfirm_name
                    }, callback);
                }
            });
        });
        
        table.on('tool(lawyer)', function(obj){
            var data = obj.data;
            if(obj.event === 'setMain') {
                layer.confirm('确认设为主办律师？', {icon: 3, title:'提示'}, function(index){
                    let callback = function(e) {
                        layer.msg(e.msg);
                        if(e.code === 0) {
                            layui.lawyerTable.reload();
                        }
                    };
                    tool.post("/lawcase/api/setMainLawyer", {
                        case_id: $('#case_id').val(),
                        lawyer_id: data.lawyer_id
                    }, callback);
                    layer.close(index);
                });
            } else if(obj.event === 'delete') {
                layer.confirm('确认移除该律师？', {icon: 3, title:'提示'}, function(index){
                    let callback = function(e) {
                        layer.msg(e.msg);
                        if(e.code === 0) {
                            layui.lawyerTable.reload();
                        }
                    };
                    tool.delete("/lawcase/api/delLawyer", {
                        case_id: $('#case_id').val(),
                        lawyer_id: data.lawyer_id
                    }, callback);
                    layer.close(index);
                });
            }
        });
    });
}

</script>